package com.jxtc.enterprise.delivery.controller;

import com.alibaba.fastjson.JSONObject;
import com.jxtc.enterprise.common.utils.Result;
import com.jxtc.enterprise.common.utils.ResultGenerator;
import com.jxtc.enterprise.delivery.service.RiderVerifyOrderService;
import com.jxtc.enterprise.delivery.vo.verifyOrder.VerifyOrderDetailVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/jxtc/enterprise/delivery/verifyOrder")
@RequiredArgsConstructor
@Slf4j
public class RiderVerifyOrderController {

    private final RiderVerifyOrderService verifyOrderService;

    /**
     * 6.5.1 根据取餐码，查询订单详情
     * @param pickupCode 4位数字取餐码，比如 8976
     * @param request HttpServlet接口的实现类，请求对象
     * @return 当天订单中， 取餐码为入参取餐码的订单列表
     */
    @GetMapping("/queryOrderDetailsByPickupCode")
    public Result<List<VerifyOrderDetailVO>> queryOrderDetailsByPickupCode(@RequestParam("pickupCode") Integer pickupCode, HttpServletRequest request) {
        String riderId = (String) request.getAttribute("userId");
        log.info("/queryOrderDetailsByPickupCode, riderId: {}, pickupCode: {}", riderId, pickupCode);
        List<VerifyOrderDetailVO> vo = verifyOrderService.queryOrderDetailsByPickupCode(riderId, pickupCode);
        log.info("/queryOrderDetailsByPickupCode, response: {}", JSONObject.toJSONString(vo));
        return ResultGenerator.genSuccessResult(vo);
    }

    /**
     * 6.5.2 确认核销
     * @param orderId
     * @return
     */
    @GetMapping("/confirmUserOrderReceived")
    public Result<Void> confirmUserOrderReceived(@RequestParam("orderId") String orderId) {
        log.info("/confirmUserOrderReceived, orderId: {}", orderId);
        verifyOrderService.confirmUserOrderReceived(orderId);
        return ResultGenerator.genSuccessResult();
    }

}